(function () {
    //获取页面传过来的参数
    var url = decodeURI(location.search); //?id="123456"&Name="bicycle";
    var object = {};
    if (url.indexOf("?") != -1)//url中存在问号，也就说有参数。
    {
        var str = url.substr(1);  //得到?后面的字符串
        var strs = str.split("&");  //将得到的参数分隔成数组[id="123456",Name="bicycle"];
        for (var i = 0; i < strs.length; i++) {
            object[strs[i].split("=")[0]] = strs[i].split("=")[1]
        }
    }

    // console.log(object);

    $.ajax({
        url: "/system/mTelRcd/telHistoryByAddress",
        data: {fmAddress: object.fmAddress},
        type: "POST",
        dataType: "json",
        beforeSend: function () {
            $('#loading').modal('show');
        },
        complete: function () {
            $('#loading').modal('hide');
        },
        success: function (data) {
            // data = jQuery.parseJSON(data);  //dataType指明了返回数据为json类型，故不需要再反序列化
            var contentObj = document.getElementsByClassName("content")[0];
            if(data.rows.length==0){
                contentObj.innerHTML = '没有数据';
                $('#expandBtn').parent().hide();
            }else {
                var isHtml = "";
                var arr = data.rows;

                for (var i = 0; i < data.rows.length; i++) {
                    let wkFmNo = arr[i].wkFmNo == null ? '暂无' : arr[i].wkFmNo;
                          // console.log(arr[i])
                    isHtml += '<div class="col-sm-12 work-box" ondblclick="parent.telDetail('+arr[i].wkPkId+','+arr[i].pkId+')">';
                    isHtml += '<div class="work-box-top">';
                    isHtml += '<div class="col-sm-12 work-box-top-box">';
                    isHtml += '<div class="col-sm-10"><a class="work-box-a" onclick="parent.telDetail('+arr[i].wkPkId+','+arr[i].pkId+')"><span>'+arr[i].fromName+'[' + arr[i].dfmClaName +']'+arr[i].dcntAname+'</span></a></div>';
                    isHtml += '<div class="col-sm-2">';
                    isHtml += '<button type="button" class="btn btn-sm btn-primary" onclick="copy('+'\''+arr[i].pkId+'\''+')" style="color: #206ef7;background-color: white;margin-right: 15px;">一键复制';
                    isHtml += '</button>';
                    // isHtml += '<i class="fa fa-chevron-circle-right"></i>';
                    isHtml += '</div>';
                    isHtml += '</div>';
                    isHtml += '<div class="col-sm-12 work-box-top-class" onclick="parent.telDetail('+arr[i].wkPkId+','+arr[i].pkId+')">'
                    isHtml += '<div class="col-sm-6"><a style="color: inherit">流水号：'+arr[i].wsCode+'</a></div>'
                    isHtml += '<div class="col-sm-6"><a style="color: inherit">工单编号：'+wkFmNo +'</a></div>'
                    isHtml += '</div>'
                    isHtml += '<div class="col-sm-12 work-box-class" onclick="parent.telDetail('+arr[i].wkPkId+','+arr[i].pkId+')">';
                    isHtml += '<a style="color: inherit">'+ arr[i].fmContent + '</a>'
                    isHtml += '</div>'
                    isHtml += '<div class="col-sm-12 work-box-buttom">';
                    isHtml += '<div class="col-sm-6">' + arr[i].fromTime + '</div>';
                    isHtml += '<div class="col-sm-4">';
                    isHtml += '<div class="col-sm-12 entering-pd">处理情况：'+arr[i].stateDesc+'</div>';
                    isHtml += '</div>';
                    isHtml += '</div>';
                    isHtml += '</div>';
                    isHtml += '</div>';
                };
                // 一页有多少条数据
                var size = 5;
                // 当前页码
                var curPage = 1;
                // 总共有多少页
                var totalPage = Math.floor((data.total / size) + 1);
                // 是否有下一页
                var hasNext = curPage != totalPage;
                if(hasNext){
                    isHtml += '<div id="txtMore'+curPage+'" style="text-align: center" onclick="more('+totalPage+','+size+','+(curPage+1)+')"><a>更多</a></div>'
                }else{
                    isHtml += '<div style="text-align: center">没有更多了......</div>'
                }
                contentObj.innerHTML = isHtml;
            }

        }

    });


    // //关键字
    // for (var i = 0; i < document.getElementsByClassName("work-box-a").length; i++) {
    //     var str = document.getElementsByClassName("work-box-a")[i].innerHTML;
    //     var keyWord = "投诉举报";
    //     if (str != null && keyWord != null) {
    //         var substr = "/" + keyWord + "/g";
    //         var replaceStr = str.replace(eval(substr), "<span style='color:#ff0000;font-weight:bold'>" + keyWord + "</span>")
    //         // console.log(replaceStr);
    //         document.getElementsByClassName("work-box-a")[i].innerHTML = replaceStr;
    //     }
    // }


    copy=function(val) {
        $.ajax({
            cache: true,
            type: "POST",
            url:  "/system/mTelRcd/copy",
            data: {
                id: val
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                parent.copy(0,data);
            }
        });
    }

    more = function(totalPage,size,curPage) {
        $('#txtMore'+(curPage - 1)).fadeOut('fast',function(){
            $.ajax({
                url: "/system/mTelRcd/telOver",
                data: {pageSize:size,pageNum:curPage,fromTelA: object.fromTelA},
                type: "POST",
                dataType: "json",
                success: function (data) {
                    // data = jQuery.parseJSON(data);  //dataType指明了返回数据为json类型，故不需要再反序列化
                    var contentObj = document.getElementsByClassName("content")[0];
                    var isHtml = contentObj.innerHTML;
                    var arr = data.rows;

                    var hasNext = totalPage != curPage;

                    for (var i = 0; i < data.rows.length; i++) {
                        let wkFmNo = arr[i].wkFmNo == null ? '暂无' : arr[i].wkFmNo;
                        // console.log(arr[i])
                        isHtml += '<div class="col-sm-12 work-box" ondblclick="parent.telDetail('+arr[i].wkPkId+','+arr[i].pkId+')">';
                        isHtml += '<div class="work-box-top">';
                        isHtml += '<div class="col-sm-12 work-box-top-box">';
                        isHtml += '<div class="col-sm-10"><a class="work-box-a" onclick="parent.telDetail('+arr[i].wkPkId+','+arr[i].pkId+')"><span>'+arr[i].fromName+'[' + arr[i].dfmClaName +']'+arr[i].dcntAname+'</span></a></div>';
                        isHtml += '<div class="col-sm-2">';
                        isHtml += '<button type="button" class="btn btn-sm btn-primary" onclick="copy(0,'+'\''+arr[i].pkId+'\''+')" style="color: #206ef7;background-color: white;margin-right: 15px;">一键复制';
                        isHtml += '</button>';
                        // isHtml += '<i class="fa fa-chevron-circle-right"></i>';
                        isHtml += '</div>';
                        isHtml += '</div>';
                        isHtml += '<div class="col-sm-12 work-box-top-class" onclick="parent.telDetail('+arr[i].wkPkId+','+arr[i].pkId+')">'
                        isHtml += '<div class="col-sm-6"><a style="color: inherit">流水号：'+arr[i].wsCode+'</a></div>'
                        isHtml += '<div class="col-sm-6"><a style="color: inherit">工单编号：'+wkFmNo +'</a></div>'
                        isHtml += '</div>'
                        isHtml += '<div class="col-sm-12 work-box-class" onclick="parent.telDetail('+arr[i].wkPkId+','+arr[i].pkId+')">';
                        isHtml += '<a style="color: inherit">'+ arr[i].fmContent + '</a>'
                        isHtml += '</div>'
                        isHtml += '<div class="col-sm-12 work-box-buttom">';
                        isHtml += '<div class="col-sm-6">' + arr[i].fromTime + '</div>';
                        isHtml += '<div class="col-sm-4">';
                        isHtml += '<div class="col-sm-12 entering-pd">处理情况：'+arr[i].stateDesc+'</div>';
                        isHtml += '</div>';
                        isHtml += '</div>';
                        isHtml += '</div>';
                        isHtml += '</div>';
                    };
                    if(hasNext){
                        isHtml += '<div id="txtMore'+curPage+'" style="text-align: center" onclick="more('+totalPage+','+size+','+(curPage+1)+')"><a>更多</a></div>'
                    }else{
                        isHtml += '<div style="text-align: center">没有更多了......</div>'
                    }
                    contentObj.innerHTML = isHtml;

                }

            });
        });
    }


})();

